<template>
  <div>
    <!-- 按钮组 -->
    <el-button-group style="margin-bottom: 15px">
      <el-button type="primary" size="small" @click="addQuestion">新增问题</el-button>
    </el-button-group>
    <!-- {{classifyMap}} -->
    <!-- 表格 -->
    <el-table border :data="questions" stripe max-height="500" style="width: 100%">
      <el-table-column label="#" type="index" width="50"> </el-table-column>
      <el-table-column prop="title" label="题目">
        <template slot-scope="scope">
          <el-link type="primary" @click="showDetails(scope.row)">{{
            scope.row.title
          }}</el-link>
        </template>
      </el-table-column>
      <el-table-column prop="uid" label="作者" width="100"> </el-table-column>
      <el-table-column
        prop="cid"
        :formatter="(row) => classifyMap[row.cid]"
        width="180"
        label="分类"
      >
      </el-table-column>
      <el-table-column prop="created" width="180" label="发布时间"> </el-table-column>

      <el-table-column fixed="right" label="操作" width="150">
        <template slot-scope="scope">
          <el-button type="warning" size="small" @click="updataData(scope.row)"
            >修改</el-button
          >
          <el-button type="danger" size="small" @click="deldataData(scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <!-- 抽屉 -->
    <el-drawer
      title="我是标题"
      size="50%"
      :visible.sync="drawer"
      :with-header="false"
      :wrapperClosable="false"
    >
      <div style="padding: 20px">
        <el-page-header @back="drawer = false" title="关闭"> </el-page-header>
        <el-form
          :model="form"
          :rules="rules"
          ref="form"
          label-width="100px"
          class="demo-form"
          :disabled="disabledForm === 'showdetails'"
        >
          <el-form-item label="题目" prop="title">
            <el-input
              type="textarea"
              autosize
              placeholder="请输入内容"
              v-model="form.title"
            >
            </el-input>
          </el-form-item>
          <el-form-item label="分类" prop="cid">
            <el-select v-model="form.cid" placeholder="请选择" size="small">
              <el-option
                v-for="(item, key) in classifyMap"
                :key="key"
                :label="item"
                :value="key * 1"
              >
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="选项" prop="options">
            <el-checkbox-group v-model="form.answers">
              <div class="options" v-for="(item, index) in form.options" :key="index">
                <el-checkbox :label="getLetterBindexyIndex(index)"></el-checkbox>
                <el-input
                  type="textarea"
                  autosize
                  placeholder="请输入内容"
                  v-model="item.value"
                >
                </el-input>
                <template v-if="index == form.options.length - 1">
                  <el-button
                    @click="addOptions"
                    type="success"
                    icon="el-icon-plus"
                    circle
                    size="small"
                  ></el-button>
                </template>
                <el-button
                  @click="delOptions(index)"
                  type="danger"
                  v-if="index > 3"
                  icon="el-icon-minus"
                  circle
                  size="small"
                ></el-button>
              </div>
            </el-checkbox-group>
          </el-form-item>
          <el-form-item label="答案">
            <div>{{ doSort(form.answers).join(",") }}</div>
          </el-form-item>
          <el-form-item label="答案提示" prop="tips">
            <el-input
              v-model="form.tips"
              placeholder="请输入内容"
              type="textarea"
            ></el-input>
          </el-form-item>
        </el-form>
        <footer class="form_footer">
          <el-button
            v-if="disabledForm != 'showdetails'"
            size="small"
            type="primary"
            @click="submitForm('form')"
            >保存</el-button
          >
          <el-button size="small" @click="handelCal">取消</el-button>
        </footer>
      </div>
    </el-drawer>
  </div>
</template>

<script>
import { mapState, mapGetters } from "vuex";
export default {
  layout: "main",
  data() {
    return {
      
      form: {
        answers: [],
        options: [{ value: "" }, { value: "" }, { value: "" }, { value: "" }],
      },
      rules: {
        title: [{ required: true, message: "请输入题目", trigger: "blur" }],
        cid: [{ required: true, message: "请选择活动区域", trigger: "change" }],

        answers: [{ required: true, message: "请输入内容", trigger: "blur" }],
        tips: [{ required: true, message: "请输入内容", trigger: "blur" }],
      },
      drawer: false,
    };
  },
  fetch({ $axios, store }) {
    store.dispatch("question/randerQuestionTable");
    store.dispatch("classify/getClaccifyData", $axios);
  },
  computed: {
    ...mapState("question", ["questions", "disabledForm"]),
    ...mapGetters("classify", ["classifyMap"]),
  },
  methods: {
    // 详情showDetails
    showDetails(row) {
      this.drawer = true;
      this.form = {
        ...row,
        answers: row.answers.split(","),
        options: JSON.parse(row.options),
      };
      this.$store.commit("question/SET_STATUS", "showdetails");
    },
    // 修改
    async updataData(row) {
      this.drawer = true;
      console.log(row);
      this.form = {
        ...row,
        answers: row.answers.split(","),
        options: JSON.parse(row.options),
      };
      this.$store.commit("question/SET_STATUS", "update");
    },
    // 删除handeldelQuestionData
    deldataData(row) {
      if (confirm("确定删除吗")) {
        this.$store.dispatch("question/handeldelQuestionData", row);
        this.$message({
          showClose: true,
          message: "题目删除成功 ",
          type: "success",
        });
      }
    },
    // 保存
    submitForm(formName) {
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          if (this.form.id) {
            await this.$store.dispatch("question/handelupdateQuestionData", this.form);
          } else {
            await this.$store.dispatch("question/handeladdQuestionData", this.form);
          }
          this.$message({
            showClose: true,
            message: "题目保存成功 ",
            type: "success",
          });
          this.drawer = false;
          this.form = {
            answers: [],
            options: [{ value: "" }, { value: "" }, { value: "" }, { value: "" }],
          };
        }
      });
    },
    addQuestion() {
      this.drawer = true;
      this.$store.commit("question/SET_STATUS", "add");
    },
    handelCal() {
      this.drawer = false;
      this.form = {
        answers: [],
        options: [{ value: "" }, { value: "" }, { value: "" }, { value: "" }],
      };
    },
    getLetterBindexyIndex(index) {
      // console.log(index);
      //通过字母拿到索引
      return String.fromCharCode("A".charCodeAt(0) + index); //charCodeAt(0) A得charCodeAt  65  fromCharCode from(从哪里来)Char(字符)Code(码)
    },
    // 删除选项
    delOptions(index) {
      this.form.options.splice(index, 1);
      // var index = this.initOptions.indexOf(item)
      // if (index !== -1) {
      //    this.initOptions.splice(index, 1)
      // }
      //  this.initOptions.forEach((v,i)=>{
      //    v.text = this.answers[i];
      //  })
    },
    // 增加选项
    addOptions() {
      this.form.options.push({ value: "" });
      // this.initOptions.push({ value: "",text:this.answers[this.initOptions.length]})
    },
    // 答案
    doSort(arr) {
      // console.log(arr);
      return [...arr].sort();
    },
  },
};
</script>

<style scoped>
.options {
  display: flex;
  align-items: center;
}
.el-checkbox:last-of-type {
  margin-right: 15px;
}
.options .el-textarea {
  width: 500px;
  margin-right: 15px;
}
label.el-checkbox {
  width: 32px;
}
.form_footer {
  padding-left: 100px;
}
</style>
<style>
.el-drawer.rtl {
  overflow: scroll;
}
</style>
